package test;

import com.edu.bean.Emp;
import com.edu.bean.Grade;
import com.edu.bean.Student;
import com.edu.mapper.EmpMapper;
import com.edu.mapper.GradeMapper;
import com.edu.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest03 {

    SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";//路径
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sessionFactory.openSession();
    }

    /*
     *   多对一: 利用map接受查询结果
     * */
    @Test
    public void test01(){
        GradeMapper gradeMapper = sqlSession.getMapper(GradeMapper.class);

        Grade grade = gradeMapper.selectGradeById(1);
        System.out.println(grade);
        List<Student> students = grade.getStudentList();
        for (Student student : students) {
            System.out.println(student);
        }

    }


    @Test
    public void test02(){
        GradeMapper gradeMapper = sqlSession.getMapper(GradeMapper.class);

        Grade grade = gradeMapper.selectGradeById2(2);

        System.out.println("--------");
        System.out.println(grade);

        for (Student student : grade.getStudentList()) {
            System.out.println(student);
        }
    }

}
